package titt.service.strategy;

import titt.head.request.MethodHead;
import titt.service.ServicePackage;
import titt.service.os.ServiceObjectServer;

public class CombinedStrategy implements TITTStrategy {
	
	private TITTStrategy a,b;
		
	public CombinedStrategy(TITTStrategy... strats) {
		this(strats, 0);
	}
	
	private CombinedStrategy(TITTStrategy[] strats, int pointer) {
		a = strats[pointer];
		b = pointer + 1 < strats.length? new CombinedStrategy(strats, pointer + 1) : null;
	}
	
	@Override
	public boolean acceptsEncoding(String enc) { 
		return a.acceptsEncoding(enc) || b.acceptsEncoding(enc);
	}

	@Override
	public void invokeAndRespond(MethodHead m, ServiceObjectServer sos, ServicePackage pack) {
		String acc = m.getHeader("Accept") != null? m.getHeader("Accept").getValue() : "*";
		
		if(b == null || a.acceptsEncoding(acc))
			a.invokeAndRespond(m, sos, pack);
		else
			b.invokeAndRespond(m, sos, pack);
	}	
	
}
